
<div class="main_bd">

    <div class="panel panel_overview">
        <div class="panel__hd">
            <h3 class="panel__title">订单列表</h3></div>
        <div class="panel__bd">

            <!--<div class="filter-box">-->
            <!--<a href="" class="btn btn_primary">新建群发</a>-->
            <!--</div>-->
            <table class="list-box cell-border hover stripe" id="Table1">


                <tfoot>

                </tfoot>
            </table>
        </div>
    </div>
</div>
<script>

    function getStatus(status){
        var str= ''
        switch (status){
            case 0:
                str = '待付款'
                break;
            case 1:
                str = '待发货'
                break;
            case 2:
                str = '待收货'
                break;
            case 5:
                str = '待评价'
            case 6:
                str = '退款/售后'
            case -1:
                str = '订单失败'
                break;
            default:
                str = '状态异常'
                break;
        }
        return str;
    }

    function taggleSatus(id,status) {
        var action = 'up';

        if(status==0){
            action = 'down'
        }
        $.post('/mall/'+action,{id:id},function(res){
            if(res.code==1){
                layer.msg('切换成功');
                grid.draw();

            }
        })
    }

    $(function(){



        window.grid = $('#Table1').DataTable({
            serverSide: true,
            paging: true,//开启分页
            lengthMenu: [ //自定义分页长度
                [ 20, 50, 100 ],
                [ '20 页', '50 页', '100页' ]
            ],
            columns: [
                {"title": "订单号", 'data': 'trade_no'},
                {"title": "商品信息", 'data': 'purchaser'},
                {"title": "订单总价", 'data': 'amount'},
                {"title": "下单用户", 'data': 'userInfo'},
                {"title": "收货地址", 'data': 'link'},
                {"title": "状态", 'data': 'status'},
                {"title": "物流信息", 'data': 'express'},
                {"title": "下单时间", 'data': 'create_time'},
                {"title": "变动时间", 'data': 'update_time'},
                {"title": "操作", 'data': 'control','width':'100px'}
            ],
            "createdRow": function (row, data, index) {
                // var mallTmpl ='',buyerTmpl = '',sendTmpl='',payStatusTmpl = '';
                //
                // var tmpl = '<a href="/mall/edit?id='+data.id+'"  class="btn btn_mini btn_primary">订单详情</a>';
                // $('td', row).eq(-1).append($(tmpl));

            },
            "ajax": {
                url: "/order",
                type: "POST",
                data: function (d) {
                    //删除多余请求参数
                    for(var key in d){
                        if(key.indexOf("columns")==0||key.indexOf("order")==0||key.indexOf("search")==0){ //以columns开头的参数删除
                            delete d[key];
                        }
                    }
                    var searchParams= {
                        "page":d.draw,
                        "size":d.length
                    };
                    //附加查询参数
                    if(searchParams){
                        $.extend(d,searchParams); //给d扩展参数
                    }
                },
                dataType : "json",
                "dataSrc": function ( json ) {
                    json.data.map(function(rows){
                        rows.thumb = '<img width="60px" src="'+rows.thumb+'"/>';

                        rows.status = getStatus(rows.status);

                        rows.userInfo = '<div><span style="font-weight: bold">昵称: </span><span>'+rows.user_nickname+'</span></div>'
                        +'<div><span style="font-weight: bold">客户编号: </span><span>['+rows.user_id+']</span></div>';

                        var purchaserInfo='';
                        rows.purchaser.map(function(purchaser){
                            purchaserInfo += '<div style="margin-bottom: 4px;font-size: 12px"><img style="padding: 0 10px" width="36px" src="'+purchaser.thumb+'" />'+purchaser.title.slice(0,16)+'..</div style="marb">';
                        })
                        rows.purchaser = purchaserInfo;

                        //收货地址
                        var link =  '<div style="margin-bottom: 2px;"><span style="font-weight: bold">联系人: </span><span>'+rows.buyer_name+'</span></div>'
                                +'<div style="margin-bottom: 2px;"><span style="font-weight: bold">联系方式: </span><span>'+rows.buyer_phone+'</span></div>'
                                +'<div style="margin-bottom: 2px;"><span style="font-weight: bold">收货地址: </span><span>'+rows.buyer_address+'</span></div>'
                                +'<div style="margin-bottom: 2px;"><span style="font-weight: bold">邮编: </span><span>'+rows.buyer_postcode+'</span></div>';
                        rows.link = link;

                        //物流
                        var express =  '<div style="margin-bottom: 2px;"><span style="font-weight: bold">快递/物流: </span><span>'+rows.send_type+'</span></div>'
                            +'<div style="margin-bottom: 2px;"><span style="font-weight: bold">单号: </span><span>'+rows.send_no+'</span></div>'
                            +'<div style="margin-bottom: 2px;"><span style="font-weight: bold">状态: </span><span>'+rows.send_status+'</span></div>'
                        rows.express = express;

                        rows.control = '<a href="/order/edit?id='+rows.id+'"  class="btn btn_primary">订单详情</a>';
                    })
                    // for ( var i=0, len=json.data.length ; i<len ; i++ ) {
                    //
                    // }
                    return json.data;
                },
                dataFilter: function (json) {
                    //json是服务器端返回的数据
                    json = JSON.parse(json);
                    console.log(json)
                    var returnData = {};
                    // returnData.draw = json.data.current_page;
                    returnData.recordsTotal = json.data.total;//返回数据全部记录
                    returnData.recordsFiltered = json.data.total;//后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = json.data.data;//返回的数据列表
                    return JSON.stringify(returnData);//这几个参数都是datatable需要的，必须要
                }
            },
            oLanguage:{ // 国际化配置
                "sProcessing" : "正在获取数据，请稍后...",
                "sLengthMenu" : "显示 _MENU_ 条",
                "sZeroRecords" : "没有找到数据",
                "sInfo" : "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
                "sInfoEmpty" : "记录数为0",
                "sInfoFiltered" : "(全部记录数 _MAX_ 条)",
                "sInfoPostFix" : "",
                "sSearch" : "查询",
                "sUrl" : "",
                "oPaginate" : {
                    "sFirst" : "第一页",
                    "sPrevious" : "上一页",
                    "sNext" : "下一页",
                    "sLast" : "最后一页"
                }
            }
        });


    })


</script>